﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraTab;
using DevExpress.XtraNavBar;
using DevExpress.XtraBars;
using System.Windows.Forms;

namespace Future.FormBase
{
    /// <summary>
    /// 模块管理器
    /// </summary>
    public class ModuleManager
    {
        private XtraTabControl _tabControlModules = null;
        /// <summary>
        /// 模块容器中的XtraTabControl控件,用于放置模块主窗体
        /// </summary>        
        public XtraTabControl TabControlModules { get { return _tabControlModules; } }

        /// <summary>
        ///MDI主窗体指针
        /// </summary>
        private Form _MDIMainForm = null;

        /// <summary>
        /// 已加载的模块列表
        /// </summary>
        private static IList<ModuleInfo> _Modules = null;

        /// <summary>
        /// 获取已加载的模块
        /// </summary>
        public static IList<ModuleInfo> GetAvailableModules()
        {
            return _Modules;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mdiMainForm">MDI主窗体指针</param>
        /// <param name="tabControlModules">模块容器中的XtraTabControl控件,用于放置模块主窗体 </param>
        public ModuleManager(Form mdiMainForm, XtraTabControl tabControlModules)
        {
            _MDIMainForm = mdiMainForm;
            _tabControlModules = tabControlModules;
        }
        /// <summary>
        /// 加载模块主方法
        /// </summary>
        public void LoadModules(StatusLable msg, MenuStrip modulesMenu)
        {
            try
            {
                ModuleLoader loader = new ModuleLoader();
                _Modules = loader.GetModuleList();//从目录装载模块
                foreach (ModuleInfo m in _Modules)
                {
                    if (!loader.LoadModule(m)) continue;

                    msg.UpdateMessage("正在加载：" + loader.GetCurrentModuleName());
                    loader.LoadGUI(_tabControlModules);
                    loader.LoadMenu(modulesMenu);
                    loader.ModuleMainForm.MdiParent = _MDIMainForm;//设置模块主窗体的父窗体.
                }
                GC.Collect();
            }
            catch (Exception ex)
            {
                Common.Msg.ShowException(ex);
            }
        }
        /// <summary>
        /// 根据菜单创建Bar按钮
        /// </summary>
        /// <param name="menuBar">Bar按钮</param>
        /// <param name="mainMenu">主菜单(各模块主菜单的组合)</param>
        public void CreateToolButtons(Bar menuBar, ToolStrip mainMenu)
        {
            foreach (ToolStripMenuItem moduleMainMenu in mainMenu.Items)
            {
                if (!moduleMainMenu.Enabled) continue;//菜单是禁止使用状态，无权限

                //模块主菜单名称(一级菜单)
                BarSubItem menuOwner = new BarSubItem(menuBar.Manager, moduleMainMenu.Text);
                menuOwner.PaintStyle = BarItemPaintStyle.CaptionGlyph;
                menuOwner.Glyph = moduleMainMenu.Image;
                menuBar.ItemLinks.Add(menuOwner);

                foreach (ToolStripItem item in moduleMainMenu.DropDownItems)
                {
                    if (item is ToolStripSeparator) continue;
                    if (!item.Enabled) continue;//菜单是禁止使用状态，无权限

                    if (item is ToolStripMenuItem && (item as ToolStripMenuItem).DropDownItems.Count > 0)
                    {
                        BarSubItem itemOwner = new BarSubItem(menuBar.Manager, item.Text);
                        menuOwner.ItemLinks.Add(itemOwner);
                        this.LoadBarSubItems(itemOwner, item as ToolStripMenuItem);
                    }
                    else
                        this.LoadBarButtonItem(menuOwner, item);
                }
            }
        }
        /// <summary>
        /// 将菜单项转换为BarButtonItem对象
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="item"></param>
        public void LoadBarButtonItem(BarSubItem owner, ToolStripItem item)
        {
            BarButtonItem button = new BarButtonItem();
            button.Caption = item.Text;
            button.Tag = item;
            button.ItemClick += new ItemClickEventHandler(OnBarButtonItemClick);
            owner.ItemLinks.Add(button);
        }
        /// <summary>
        /// BarButtonItem对象的Click事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBarButtonItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.Item.Tag != null && e.Item.Tag is ToolStripItem)
                (e.Item.Tag as ToolStripItem).PerformClick();
        }

        /// <summary>
        /// 将菜单项转换为BarButtonItem对象
        /// </summary>
        /// <param name="itemOwner"></param>
        /// <param name="menuItem"></param>
        public void LoadBarSubItems(BarSubItem itemOwner, ToolStripMenuItem menuItem)
        {
            foreach (ToolStripItem item in menuItem.DropDownItems)
            {
                if (item is ToolStripSeparator) continue;
                if (!item.Enabled) continue; //菜单是禁止使用状态，无权限

                if (item is ToolStripMenuItem && (item as ToolStripMenuItem).DropDownItems.Count > 0)
                {
                    BarSubItem subItem = new BarSubItem(itemOwner.Manager, item.Text);
                    itemOwner.ItemLinks.Add(subItem);
                    this.LoadBarSubItems(subItem, item as ToolStripMenuItem);
                }
                else
                {
                    this.LoadBarButtonItem(itemOwner, item);
                }
            }
        }

        public void CreateNavBarButtons(NavBarControl navBarControl, MenuStrip mainMenu, NavigatorStyle NavigatorStyle)
        {
            NavigatorBase creator;
            //跟据用户选择样式创建不同类型的Navigator
            if (NavigatorStyle == NavigatorStyle.BarItem)
            {
                creator = new NavigatorBarList(this);
            }
            else
            {
                creator = new NavigatorTreeView(this);
            }


            navBarControl.BeginUpdate();
            creator.CreateNavigator(mainMenu, navBarControl);
            navBarControl.EndUpdate();
        }
        /// <summary>
        /// 显示指定的模块主窗体
        /// </summary>  
        public void ActiveModule(string moduleDisplayName)
        {
            //显示模块容器子窗体
            (_MDIMainForm as IMdiForm).ShowModuleContainerForm();

            foreach (XtraTabPage page in this._tabControlModules.TabPages)
            {
                if (page.Text.Equals(moduleDisplayName))
                {
                    _tabControlModules.SelectedTabPage = page;
                }
            }
        }
    }
}
